package com.dididoctor.doctor.Activity.Order;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.MV.BaseFragment;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Ui.customlistview.OnRefreshListener;
import com.dididoctor.doctor.Ui.customlistview.RefreshListView;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallbackWrapper;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.model.RecentContact;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述： 登陆页
 * @项目名称：ddgh
 * @类名称： loginActivity
 * @包名称： com.doudoukeji.service.Activity.login
 * @创建人：张强 boy
 * @创建时间：2016/3/7 16:40
 * @修改人：
 * @修改时间：2016/3/7 16:40
 * @修改备注： new parent class(用一句话描述修改了什么)
 */

@SuppressLint("ValidFragment")
public class OrderModuleFragment extends BaseFragment implements View.OnClickListener, OrderView {
    private Activity activity;
    private int pageIndex = 1;

    private RefreshListView listView;
    private List<OrderBean> doctors = new ArrayList<OrderBean>();
    private OrderPresenter presenter;
    private OrderAdapter adapter;
    //0-待处理1-已处理
    private String type = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public OrderModuleFragment(String type) {
        this.type = type;
    }

    @Override
    public void onAttach(Activity activity) {
        this.activity = activity;
        super.onAttach(activity);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_patient_list, null);
        this.initView(rootView);
        return rootView;

    }

    private void initView(View rootView) {

        listView = (RefreshListView) rootView.findViewById(R.id.lv_forum);
        listView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onPullDownRefresh() {
                pageIndex = 1;
//                presenter.getOrderList(type, pageIndex, ls);
//                listView.onRefreshFinish();
                initData(pageIndex);
            }

            @Override
            public void onLoadMoring() {
                pageIndex++;
//                presenter.getOrderList(type, pageIndex, ls);
//                listView.onRefreshFinish();
                initData(pageIndex);
            }

            @Override
            public void onScrollListener(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        listView.setHeadAndFoot(true, true);
        adapter = new OrderAdapter(getActivity(), doctors);
        listView.setAdapter(adapter);

    }

    @Override
    public void onResume() {
        super.onResume();
        initData(pageIndex);
    }

    public void initData(final int pageIndex) {
        presenter = new OrderPresenter(activity, this);
        NIMClient.getService(MsgService.class).queryRecentContacts()
                .setCallback(new RequestCallbackWrapper<List<RecentContact>>() {
                    @Override
                    public void onResult(int code, List<RecentContact> recents, Throwable e) {
                        if (recents != null) {
                            List<YXDoctorBean> ls = new ArrayList<YXDoctorBean>();
                            for (int x = 0; x < recents.size(); x++) {
                                YXDoctorBean yxcb = new YXDoctorBean();
                                String contactId = recents.get(x).getContactId();
                                int unreadCount = recents.get(x).getUnreadCount();
                                yxcb.setContactId(contactId);
                                if (unreadCount > 0) {
                                    yxcb.setChatstatus("0");
                                } else {
                                    yxcb.setChatstatus("1");
                                }
                                ls.add(yxcb);
                            }
                            presenter.getOrderList(type, pageIndex,ls);
                        }
                    }
                });
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void getdoctorfail() {
        showToastMessage("网络连接失败，请检查您的网络设置");
        listView.onRefreshFinish();
    }

    @Override
    public void getdoctorsucced(List<OrderBean> doctors) {
        if (doctors != null && doctors.size() > 0) {
            if (pageIndex == 1) {
                this.doctors.clear();
                adapter.setData(doctors);
                listView.onRefreshFinish();
            }else{
                if (doctors.size() < ConstantValue.LOAD_MESSAGE_COUNT) {
                    adapter.addData(doctors);
                    listView.showNoMoreData();
                } else {
                    adapter.addData(doctors);
                    listView.hideNoMoreData();
                }
            }
            adapter.notifyDataSetChanged();
        } else {
            if (pageIndex == 1) {
                listView.onRefreshFinish();
            }else{
                listView.showNoMoreData();
                adapter.notifyDataSetChanged();
            }
        }

    }

    @Override
    public void updatestatussucced() {

    }

    @Override
    public void updatesatusfail() {

    }


}
